package kr.hyunist.androidcomplete01saeildamil.ch09;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.graphics.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public class MenuCheckTest extends Activity {
	Button mBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menuchecktest);

		mBtn = (Button) findViewById(R.id.menucheckbtn1);
	}

	//xml에서 만들어둔 menu를 전개(onCreeateOptionMenu)
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menucheckex, menu);

		return true;
	}

	//menu를 선택했을때 이벤트 처리
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);

		switch (item.getItemId()) {
		case R.id.bigfont:
			if(item.isChecked()){
				mBtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
			} else {
				mBtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, 40);
			}
			return true;

		case R.id.red:
			mBtn.setTextColor(Color.RED);
			return true;

		case R.id.green:
			mBtn.setTextColor(Color.GREEN);
			return true;

		case R.id.blue:
			mBtn.setTextColor(Color.BLUE);
			return true;
		}

		return false;
	}
	
	//버튼의 현재 속성을 읽어 메뉴의 상태 갱신
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {		
		if(mBtn.getTextSize() == 40){
			menu.findItem(R.id.bigfont).setChecked(true);
		} else {
			menu.findItem(R.id.bigfont).setChecked(false);
		}
		
		int color = mBtn.getTextColors().getDefaultColor();
		
		if(color == Color.RED){
			menu.findItem(R.id.red).setChecked(true);
		}
		if(color == Color.GREEN){
			menu.findItem(R.id.green).setChecked(true);
		}
		if(color == Color.BLUE){
			menu.findItem(R.id.blue).setChecked(true);
		}
		
		return true;
	}
}
